package com.hq.controller;

import com.hq.entity.QuestionType;
import com.hq.entity.ResultVo;
import com.hq.service.QuestionTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/type")
@Slf4j
public class QuestionTypeController {

    @Autowired
    private QuestionTypeService questionTypeService;

    // 查询所有题型
    @RequestMapping("/all")
    public List<QuestionType> all(){
        log.info("查询所有题型");
        return questionTypeService.findAll();
    }

    // 分页查询
    @RequestMapping("/list")
    public Map<String,Object> list(int page, int size){
        log.info("分页查询");
        return questionTypeService.findByPage(page,size);
    }

    // 添加题型
    @RequestMapping("/add")
    public ResultVo add(@RequestBody QuestionType questionType){
        log.info("添加的题型{}",questionType.getTypeName());
        ResultVo vo=new ResultVo();
        if(questionTypeService.addQuestionType(questionType)>0){
            vo.setCode(200);
            vo.setMsg("添加成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("添加失败！");
        }
        return vo;
    }

    // 单行删除题型
    @RequestMapping("/del")
    public ResultVo del(int tid){
        log.info("删除的题型id{}",tid);
        ResultVo vo=new ResultVo();
        if(questionTypeService.delType(tid)>0){
            vo.setCode(200);
            vo.setMsg("删除成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("删除失败！");
        }
        return vo;
    }

    // 批量删除题型
    @RequestMapping("/dels")
    public ResultVo deltypes(String ids){
        log.info("批量删除的题型id{}",ids);
        ResultVo vo=new ResultVo();
        if(questionTypeService.delTypes(ids)>0){
            vo.setCode(200);
            vo.setMsg("删除成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("删除失败！");
        }
        return vo;
    }

    //编辑题型
    @RequestMapping("/edit")
    public ResultVo edit(@RequestBody QuestionType questionType){
        log.info("修改的题型{}",questionType.getTypeName());
        ResultVo vo=new ResultVo();
        if(questionTypeService.editQuestionType(questionType)>0){
            vo.setCode(200);
            vo.setMsg("修改成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("修改失败！");
        }
        return vo;
    }
}
